﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Common;
using System.Web.Script.Serialization;
using Basic.VO;
using Basic.Business;

public partial class Basic_Background_AdminManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //强制刷新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache"); 

        //判断用户是否登录
        if (!Tools.IsAdminSessionValid())
        {
            Tools.ShowInfo(StringTable.ErrorSessionTimeOut, "../Background/AdminLogin.aspx", "_self");
        }

        //处理AJAX
        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        {
            Result<Admin> result = new Result<Admin>();
            JavaScriptSerializer json = new JavaScriptSerializer();
            string action = BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                case "saveAddAdmin":
                    try
                    {
                        if (!RoleAdmin.CheckRole("系统用户管理", (Basic.VO.Admin)Session["admin"]))
                            throw new Exception("你没有这个权限");
                        string adminName = BadChar.ReplaceBadChar(Request.Form["adminName"].Trim());
                        string adminPass = BadChar.ReplaceBadChar(Request.Form["adminPass"].Trim());
                        string confirmPassword = BadChar.ReplaceBadChar(Request.Form["confirmPassword"].Trim());
                        int adminGroupId = int.Parse(Request.Form["adminGroupId"]);
                        if (String.IsNullOrEmpty(adminName))
                            throw new Exception("用户名不能为空");
                        if (String.IsNullOrEmpty(adminPass))
                            throw new Exception("密码不能为空");
                        if (!adminPass.Equals(confirmPassword))
                            throw new Exception("两次密码不一致");
                        AdminAdmin.AddAdmin(adminName, Tools.MD5Encrypt(adminPass), adminGroupId);
                        result.error = false;
                        result.msg = "添加系统用户成功！";
                    }
                    catch (Exception ex)
                    {
                        result.error = true;
                        result.msg = ex.Message;
                    }
                    break;
                case "deleteAdmin":
                    try
                    {
                        if (!RoleAdmin.CheckRole("系统用户管理", (Basic.VO.Admin)Session["admin"]))
                            throw new Exception("你没有这个权限");
                        string adminIds = Request.QueryString["adminIds"];
                        int[] adminId = Tools.DeleteIntArrayRepeat(Tools.StringArrayToIntArray(adminIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));
                        AdminAdmin.DeleteAdmin(adminId);
                        result.error = false;
                        result.msg = "删除系统用户成功！";
                    }
                    catch (Exception ex)
                    {
                        result.error = true;
                        result.msg = ex.Message;
                    }
                    break;
                case "updateAdmin":
                    try
                    {
                        if (!RoleAdmin.CheckRole("系统用户管理", (Basic.VO.Admin)Session["admin"]))
                            throw new Exception("你没有这个权限");
                        int adminId = int.Parse(Request.Form["adminId"]);
                        string adminPass = String.Empty;
                        string confirmPassword=String.Empty;
                        if (!String.IsNullOrEmpty(Request.Form["adminPass"].Trim()))
                        {
                            adminPass = BadChar.ReplaceBadChar(Request.Form["adminPass"].Trim());
                            confirmPassword = BadChar.ReplaceBadChar(Request.Form["confirmPassword"].Trim());
                        }
                        int adminGroupId = int.Parse(Request.Form["adminGroupId"]);
                        if (!adminPass.Equals(confirmPassword)&&String.IsNullOrEmpty(adminPass))
                            throw new Exception("两次密码不一致");
                        if(!String.IsNullOrEmpty(adminPass))
                            adminPass=Tools.MD5Encrypt(adminPass);
                        AdminAdmin.Update(adminId, adminPass, adminGroupId);
                        result.error = false;
                        result.msg = "修改系统用户成功！";
                    }
                    catch (Exception ex)
                    {
                        result.error = true;
                        result.msg = ex.Message;
                    }
                    break;
                case "search":
                    try
                    {
                        if (!RoleAdmin.CheckRole("系统用户管理", (Basic.VO.Admin)Session["admin"]))
                            throw new Exception("你没有这个权限");
                        string adminName = String.Empty;
                        int adminGroupId = -1;
                        if(!String.IsNullOrEmpty(Request.QueryString["adminName"]))
                            adminName= BadChar.ReplaceBadChar(Request.QueryString["adminName"].Trim());
                        if (!String.IsNullOrEmpty(Request.QueryString["adminGroupId"]))
                            adminGroupId = int.Parse(Request.QueryString["adminGroupId"]);
                        int page = int.Parse(Request["page"]);
                        int perPage = int.Parse(Request["perpage"]);
                        result = AdminAdmin.GetAllByCondition(adminName, adminGroupId, (page - 1) * perPage, perPage);
                        result.page = page;
                        result.error = false;
                        result.msg = "获取系统用户列表成功！";
                    }
                    catch (Exception ex)
                    {
                        result.error = true;
                        result.msg = ex.Message;
                    }
                    break; 
                default:
                    result.error = true;
                    result.msg = StringTable.ErrorUnknowAction;
                    break;
            }
            Response.Write(json.Serialize(result));
            Response.End();
        }
    }
}
